package short_semester.questionnaire_planet.service.editService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import short_semester.questionnaire_planet.dao.QuestionSequenceDao;
import short_semester.questionnaire_planet.dao.SingleChoiceOptionDao;
import short_semester.questionnaire_planet.dao.SingleChoiceQuestionDao;
import short_semester.questionnaire_planet.dao.SingleChoiceQuestionRecordDao;
import short_semester.questionnaire_planet.pojo.*;
import short_semester.questionnaire_planet.request.editRequest.EditQuestionBody;
import short_semester.questionnaire_planet.request.editRequest.OptionBody;
import short_semester.questionnaire_planet.response.CommonResponse;
import short_semester.questionnaire_planet.response.editResponse.RelationResponse;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SingleChoiceQuestionService {
    
    @Autowired
    SingleChoiceQuestionDao questionDao;
    
    @Autowired
    SingleChoiceOptionDao optionDao;
    
    @Autowired
    QuestionSequenceDao sequenceDao;

    @Autowired
    SingleChoiceQuestionRecordDao recordDao;
    
    @Autowired
    RelationService relationService;

    
    public List<SingleChoiceQuestion> getQuestions(Questionnaire questionnaire){
        List<SingleChoiceQuestion> questions = questionDao.findByQuestionnaire(questionnaire);
        for(SingleChoiceQuestion question:questions){
            question.setOptions(optionDao.findBySingleChoiceQuestionOrderBySequenceAsc(question));
        }
        return questions;
    }
    
    public SingleChoiceQuestion getQuestion(Integer questionId){
        SingleChoiceQuestion question = questionDao.getById(questionId);
        question.setOptions(optionDao.findBySingleChoiceQuestionOrderBySequenceAsc(question));
        return question;
    }

    public SingleChoiceQuestion getQuestionRandom(Integer questionId){
        SingleChoiceQuestion question = questionDao.getById(questionId);
        question.setOptions(optionDao.findBySingleChoiceQuestion(question));
        if(question.getIsRandom()==null){

        }else if(question.getIsRandom()){
            Collections.shuffle(question.getOptions());
        }
        return question;
    }
    
    public SingleChoiceQuestion getQuestion(SingleChoiceQuestion question){
        question.setOptions(optionDao.findBySingleChoiceQuestionOrderBySequenceAsc(question));
        return question;
    }
    
    public List<SingleChoiceQuestion> getQuestionInfo(Questionnaire questionnaire){
        return questionDao.findByQuestionnaire(questionnaire);
    }
    
    public CommonResponse getQuestionResponse(Integer id){
        CommonResponse response = new CommonResponse();
        
        try {
            SingleChoiceQuestion question = questionDao.getById(id);
            question.setOptions(optionDao.findBySingleChoiceQuestionOrderBySequenceAsc(question));
            response.setData(question);
        }catch (Exception e){
            response.error(e.getMessage());
        }
        
        return response;
    }
    
    public List<SingleChoiceOption> getOptions(SingleChoiceQuestion question){
        return optionDao.findBySingleChoiceQuestion(question);
    }
    
    public int addQuestion(Questionnaire questionnaire,Integer sequence,String qtype){
        SingleChoiceQuestion question = new SingleChoiceQuestion(qtype);
        question.setQuestionnaire(questionnaire);
        question.setSequence(sequence);
        question = questionDao.save(question);
        
        QuestionSequence questionSequence = new QuestionSequence();
        questionSequence.setQuestionnaire(questionnaire);
        questionSequence.setSequence(sequence);
        questionSequence.setType(question.getType());
        questionSequence.setQuestionId(question.getId());
        sequenceDao.save(questionSequence);
        
        return question.getId();
    }
    
    public int getQuestionNum(Questionnaire questionnaire){
        return questionDao.countByQuestionnaire(questionnaire);
    }
    
    public void deleteQuestion(Integer questionId){
        SingleChoiceQuestion question = questionDao.findById(questionId).get();
//        for (SingleChoiceOption option: optionDao.findBySingleChoiceQuestion(question)){
//            optionDao.delete(option);
//        }
        optionDao.deleteBySingleChoiceQuestion(question);
        QuestionSequence questionSequence = sequenceDao.findByTypeAndQuestionId(question.getType(), question.getId());
        sequenceDao.delete(questionSequence);

        relationService.relationDao.deleteByQuestionIdAndType(questionId,question.getType());
        relationService.relationDao.deleteByRelationIdAndRelationType(questionId,question.getType());
        
        questionDao.delete(question);
    }
    
    public SingleChoiceQuestion getQuestionById(Integer questionId){
        return questionDao.getById(questionId);
    }
    
    public void setSequenceById(Integer questionId,Integer sequence){
        SingleChoiceQuestion question = questionDao.getById(questionId);
        question.setSequence(sequence);
        questionDao.save(question);

        QuestionSequence questionSequence = sequenceDao.findByTypeAndQuestionId(question.getType(), questionId);
        questionSequence.setSequence(sequence);
        sequenceDao.save(questionSequence);
    }
    
    public CommonResponse editQuestion(EditQuestionBody body){
        CommonResponse response = new CommonResponse();
        
        try{
            SingleChoiceQuestion question = questionDao.getById(body.getId());
            question.setValueFrom(body);
            questionDao.save(question);
            
            
            List<SingleChoiceOption> options = optionDao.findBySingleChoiceQuestion(question);
            Map<Integer, SingleChoiceOption> optionMap = new HashMap<>();
            
            for (SingleChoiceOption option :options){
                optionMap.put(option.getId(),option);
            }
            int sequence = 0;
            for (OptionBody optionBody: body.getOptions()){
                SingleChoiceOption option = new SingleChoiceOption(optionBody);
                option.setSingleChoiceQuestion(question);
                option.setSequence(sequence);
                ++sequence;

                optionMap.remove(option.getId());
                
                optionDao.save(option);
            }
            
            for (SingleChoiceOption option: optionMap.values()){
                relationService.relationDao.deleteByOptionIdAndRelationType(option.getId(),question.getType());
                optionDao.delete(option);
                
            }
        }catch (Exception e){
            response.error(e.getMessage());
        }
        
        return response;
    }
    
    public void copyQuestion(Questionnaire questionnaire,Questionnaire oldQuestionnaire){
        for (SingleChoiceQuestion question : questionDao.findByQuestionnaire(oldQuestionnaire)) {
            //拷贝基本信息
            SingleChoiceQuestion singleChoiceQuestion = new SingleChoiceQuestion(question);
            //设置问卷外键
            singleChoiceQuestion.setQuestionnaire(questionnaire);
            singleChoiceQuestion = questionDao.save(singleChoiceQuestion);
  

            QuestionSequence questionSequence = new QuestionSequence(singleChoiceQuestion);
            sequenceDao.save(questionSequence);

            for (SingleChoiceOption option : optionDao.findBySingleChoiceQuestion(question)) {
                SingleChoiceOption singleChoiceOption = new SingleChoiceOption(option);
                singleChoiceOption.setSingleChoiceQuestion(singleChoiceQuestion);
                singleChoiceOption = optionDao.save(singleChoiceOption);

            }
            
        }
    }

    //彻底删除题目，包括选项与答题
    public void completeDelete(SingleChoiceQuestion singleChoiceQuestion){
        recordDao.deleteBySingleChoiceQuestion(singleChoiceQuestion);
        optionDao.deleteBySingleChoiceQuestion(singleChoiceQuestion);
        QuestionSequence questionSequence = sequenceDao.findByTypeAndQuestionId(singleChoiceQuestion.getType(), singleChoiceQuestion.getId());
        sequenceDao.delete(questionSequence);

        questionDao.delete(singleChoiceQuestion);
    }

    //删除题目对应的回答
    public void deleteAnswer(SingleChoiceQuestion singleChoiceQuestion){
        recordDao.deleteBySingleChoiceQuestion(singleChoiceQuestion);
    }

    //添加报名默认题
    public void addSignQuestion(Questionnaire questionnaire){
        SingleChoiceQuestion singleChoiceQuestion = new SingleChoiceQuestion();
        singleChoiceQuestion.setTitle("报名选择");
        singleChoiceQuestion.setQuestionnaire(questionnaire);
        singleChoiceQuestion.setSequence(2);
        singleChoiceQuestion.setQtype("sign");

        questionDao.save(singleChoiceQuestion);
        QuestionSequence questionSequence = new QuestionSequence(singleChoiceQuestion);
        sequenceDao.save(questionSequence);

        SingleChoiceOption noSign = new SingleChoiceOption();
        noSign.setText("不参加");
        noSign.setSingleChoiceQuestion(singleChoiceQuestion);
        noSign.setSequence(0);
        noSign.setStore(10);
        noSign.setAmount(10);
        optionDao.save(noSign);

        SingleChoiceOption Sign = new SingleChoiceOption();
        Sign.setText("参加");
        Sign.setSingleChoiceQuestion(singleChoiceQuestion);
        Sign.setSequence(1);
        Sign.setStore(10);
        Sign.setAmount(10);
        optionDao.save(Sign);
    }

    //添加体温单选题
    public void addTemperatureQuestion(Questionnaire questionnaire){
        SingleChoiceQuestion singleChoiceQuestion = new SingleChoiceQuestion();
        singleChoiceQuestion.setTitle("体温");
        singleChoiceQuestion.setQuestionnaire(questionnaire);
        singleChoiceQuestion.setSequence(2);

        questionDao.save(singleChoiceQuestion);
        QuestionSequence questionSequence = new QuestionSequence(singleChoiceQuestion);
        sequenceDao.save(questionSequence);

        SingleChoiceOption singleChoiceOption1 = new SingleChoiceOption();
        singleChoiceOption1.setText("36°C及以下");
        singleChoiceOption1.setSingleChoiceQuestion(singleChoiceQuestion);
        singleChoiceOption1.setSequence(0);
        optionDao.save(singleChoiceOption1);

        SingleChoiceOption singleChoiceOption2 = new SingleChoiceOption();
        singleChoiceOption2.setText("36.0°C~37.2°C");
        singleChoiceOption2.setSingleChoiceQuestion(singleChoiceQuestion);
        singleChoiceOption2.setSequence(1);
        optionDao.save(singleChoiceOption2);

        SingleChoiceOption singleChoiceOption3 = new SingleChoiceOption();
        singleChoiceOption3.setText("37.3°C~38.0°C");
        singleChoiceOption3.setSingleChoiceQuestion(singleChoiceQuestion);
        singleChoiceOption3.setSequence(2);
        optionDao.save(singleChoiceOption3);

        SingleChoiceOption singleChoiceOption4  = new SingleChoiceOption();
        singleChoiceOption4.setText("38.1°C及以上");
        singleChoiceOption4.setSingleChoiceQuestion(singleChoiceQuestion);
        singleChoiceOption4.setSequence(3);
        optionDao.save(singleChoiceOption4);
    }

    //添加有无去过高风险地区题目
    public void addEnvironmentQuestion(Questionnaire questionnaire){
        SingleChoiceQuestion singleChoiceQuestion = new SingleChoiceQuestion();
        singleChoiceQuestion.setTitle("14天内有无去过高风险地区");
        singleChoiceQuestion.setQuestionnaire(questionnaire);
        singleChoiceQuestion.setSequence(3);

        questionDao.save(singleChoiceQuestion);
        QuestionSequence questionSequence = new QuestionSequence(singleChoiceQuestion);
        sequenceDao.save(questionSequence);

        SingleChoiceOption hasGone = new SingleChoiceOption();
        hasGone.setText("是");
        hasGone.setSingleChoiceQuestion(singleChoiceQuestion);
        hasGone.setSequence(0);
        optionDao.save(hasGone);

        SingleChoiceOption hasNotGone = new SingleChoiceOption();
        hasNotGone.setText("否");
        hasNotGone.setSingleChoiceQuestion(singleChoiceQuestion);
        hasNotGone.setSequence(1);
        optionDao.save(hasNotGone);
    }

    //添加是否感染新冠症状题目
    public void addInfecttQuestion(Questionnaire questionnaire){
        SingleChoiceQuestion singleChoiceQuestion = new SingleChoiceQuestion();
        singleChoiceQuestion.setTitle("现在是否感染新冠症状");
        singleChoiceQuestion.setQuestionnaire(questionnaire);
        singleChoiceQuestion.setSequence(4);

        questionDao.save(singleChoiceQuestion);
        QuestionSequence questionSequence = new QuestionSequence(singleChoiceQuestion);
        sequenceDao.save(questionSequence);

        SingleChoiceOption hasGone = new SingleChoiceOption();
        hasGone.setText("是");
        hasGone.setSingleChoiceQuestion(singleChoiceQuestion);
        hasGone.setSequence(0);
        optionDao.save(hasGone);

        SingleChoiceOption hasNotGone = new SingleChoiceOption();
        hasNotGone.setText("否");
        hasNotGone.setSingleChoiceQuestion(singleChoiceQuestion);
        hasNotGone.setSequence(1);
        optionDao.save(hasNotGone);
    }

    //添加投票题例子
    public void addVoteQuestion(Questionnaire questionnaire){
        SingleChoiceQuestion singleChoiceQuestion = new SingleChoiceQuestion();
        singleChoiceQuestion.setTitle("马瀚元助教帅不帅");
        singleChoiceQuestion.setQuestionnaire(questionnaire);
        singleChoiceQuestion.setSequence(0);
        singleChoiceQuestion.setQtype("vote");

        questionDao.save(singleChoiceQuestion);
        QuestionSequence questionSequence = new QuestionSequence(singleChoiceQuestion);
        sequenceDao.save(questionSequence);

        SingleChoiceOption hasGone = new SingleChoiceOption();
        hasGone.setText("帅");
        hasGone.setSingleChoiceQuestion(singleChoiceQuestion);
        hasGone.setSequence(0);
        optionDao.save(hasGone);

        SingleChoiceOption hasNotGone = new SingleChoiceOption();
        hasNotGone.setText("肯定帅");
        hasNotGone.setSingleChoiceQuestion(singleChoiceQuestion);
        hasNotGone.setSequence(1);
        optionDao.save(hasNotGone);
    }
    
    //获取题目关联关系
    public RelationResponse getRelation(Integer questionId){
        SingleChoiceQuestion singleChoiceQuestion = questionDao.getById(questionId);
        return relationService.getRelation(singleChoiceQuestion);
    }
    
}
